import { Component, OnDestroy } from '@angular/core';
import { ManhuaTableProvider } from './manhua.table.provider';

import { Subscription } from 'rxjs/Subscription';
import { Router } from '@angular/router';
import { Util } from '../../../util/util';

@Component({
    selector: 'manhua-table',
    templateUrl: './manhua.table.html',
    styleUrls: ['./manhua.table.css'],
    providers: [ManhuaTableProvider]
})


export class ManhuaTable {

    manhuas: any = [];


    page: any = {
        size: 18,
        curPage: 1,
        pages: 0,
        totalCount: 1
    };


    storeSearchParam: any = {};

    constructor(private service: ManhuaTableProvider, private router: Router, private util: Util) {

    }

    // out api
    outputHandel(searchParam, isSearchPage: boolean = false) {


        this.storeSearchParam = null;
        this.storeSearchParam = searchParam;

        // 不是搜索漫画页
        if (isSearchPage) {
            this.pageChange(1);
            return;
        }



        // 启动判断是否有缓存数据，有加载，没有从第一页开始
        let _page: any = this.util.store().get('manhuaTableParam');

        if (_page) {

            this.page = _page;

            // console.log('有缓存');
            // console.log(_page);

            this.pageChange(this.page.curPage);

        } else {

            this.pageChange(1);
        }



    }

    // go to detail page
    goDetail(manhua) {
        this.util.sessionStore().set('manhuaDetail', manhua);
        this.router.navigate(['/manhua', manhua.title]);
    }
    // page nav
    pageChange(cur) {

        this.page.curPage = cur;

        let json = Object.assign(this.storeSearchParam, this.page);

        // console.log('page page');
        // console.log(json);
        this.getHttp(json);

    }

    ngOnDestroy() {
        if (this.ajaxSub) {
            this.ajaxSub.unsubscribe();
        }

    }

    // 保存参数
    storeData() {
        this.util.store().set('manhuaTableParam', this.page);
    }

    deleteData() {
        this.util.store().delete('manhuaTableParam');
    }

    pageSet(_page) {
        this.page.pages = _page.pages;
        this.page.curPage = _page.curPage;
        this.page.totalCount = _page.totalCount;
    }

    ajaxSub: Subscription;
    getHttp(param) {
        this.ajaxSub = this.service.page(param).subscribe((res: any) => {
            if (res.success) {

                // page init
                this.pageSet(res.page);

                if (res.page.totalCount > 0) {

                    for (let i in res.data) {
                        let item = res.data[i];
                        //   设置防盗图片的中转
                        if (item.domain === "manhua.dmzj.com"||item.domain === "http://manhua.dmzj.com") {
                            item.img = "/api/manhua/img?url=" + item.img;
                        }

                    }

                    this.manhuas = res.data;

                } else {
                    this.manhuas = [];
                }
            } else {
                this.manhuas = [];
            }
        });
    }

}